<?php

namespace App\Http\Controllers\Admin;

use App\Models\Express;

class ExpressController extends BaseController
{
    /**
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @throws \Exception
     */
    public function index(){

        if ($this->isOnSubmit()) {
            $delete = $this->request->input('delete');
            if ($delete && is_array($delete)){
                foreach ($delete as $id){
                    Express::where('id', $id)->delete();
                }
            }
            $itemlist = $this->request->input('itemlist');
            if ($itemlist && is_array($itemlist)){
                foreach ($itemlist as $id=>$item){
                    if ($item['name']) {
                        if ($id > 0) {
                            Express::where('id', $id)->update($item);
                        }else {
                            Express::insert($item);
                        }
                    }
                }
            }
            return $this->messager()->message(trans('sysmessage.info save success'))->render();
        }else {

            $this->assign(['itemlist'=>Express::all()]);
            return $this->view('admin.common.express');
        }
    }
}
